package edu.flying.panda.taskmanager;

import java.util.Calendar;

import android.app.Activity;
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.app.TimePickerDialog;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.RadioButton;
import android.widget.TimePicker;

/**
 * Add Task Screen
 * @author FlyingPanda
 */

public class AddTaskScreen extends Activity {
	
	private TextView tvDisplayTime;
	private TextView tvDisplayDate;
	private Button ChangeTime;
	private Button ChangeDate;
	private Button back_button;
	private Button addTask_button;
	private Button map_button;
	private Date due;
	
	
	private int hour;
	private int minute;
	private int day;
	private int month;
	private int year;
	
	
	static final int TIME_DIALOG_ID = 999;
	static final int DATE_DIALOG_ID = 0;

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.add_task_screen);
		due = new Date();
		
		setCurrentTimeOnView();
		
		addListenerOnButton();
		
		
		back_button = (Button) findViewById(R.id.new_task_back);
		addTask_button = (Button) findViewById(R.id.new_task_add);
		map_button = (Button) findViewById(R.id.new_task_map);
		
		/**
		 * Listener for Back button, cancels Task creation and returns to Home Screen
		 */
		back_button.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {

				Intent myIntent = new Intent(v.getContext(), HomeScreen.class);
				startActivity(myIntent);
			}
		});

		/**
		 * Listener for Map button, goes to Map view
		 * Screen
		 */
		map_button.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {

				Intent myIntent = new Intent(v.getContext(), Map.class);
				myIntent.putExtra("location", ((EditText) findViewById(R.id.new_task_loc)).getText().toString());
				startActivity(myIntent);
			}
		});
		
		/**
		 * Listener for addTask button, creates a new Task and returns to Home Screen
		 */
		addTask_button.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				Log.d("addTask","1");
				String des = ((EditText) findViewById(R.id.new_task_name))
						.getText().toString();
				String location = ((EditText) findViewById(R.id.new_task_loc))
						.getText().toString();
				String fullDes = ((EditText) findViewById(R.id.new_task_comments))
						.getText().toString();
				boolean completed = ((CheckBox) findViewById(R.id.task_completed)).isChecked();
				
				Log.d("addTask","2");
				
				String type;
				if (((RadioButton) findViewById(R.id.type_school)).isChecked())
					{type = "school";}
				else if (((RadioButton) findViewById(R.id.type_personal)).isChecked())
					{type = "personal";}
				else
					{type = "work";}
				TextView errorText = (TextView) findViewById(R.id.new_task_error);
				Log.d("addTask","3");
				if (!des.equals("")) {
					Log.d("addTask","4.1");
					
					TaskManagerActivity.DB.addTask((new Task( 
							TaskManagerActivity.DB.getCURRENT_USERID(),	des, location, due,fullDes, type, completed)));
					Log.d("ATS", due.toString());
					
						Intent myIntent = new Intent(v.getContext(), HomeScreen.class);
						
						startActivity(myIntent);
						
				} else {
					Log.d("addTask","4.2");
					errorText.setText("Must enter a Task Name");
				}
				
				
				

			}
		});

	}//onCreate End
	
	
	
	// display current time
		public void setCurrentTimeOnView() {
	 
			tvDisplayTime = (TextView) findViewById(R.id.time_text);
			tvDisplayDate = (TextView) findViewById(R.id.day_text);
			Log.d("SCT", "1");
			final Calendar c = Calendar.getInstance();
			hour = c.get(Calendar.HOUR_OF_DAY);
			minute = c.get(Calendar.MINUTE);
			year = c.get(Calendar.YEAR);
			day = c.get(Calendar.DAY_OF_MONTH);
			month = c.get(Calendar.MONTH);
	 
			// set current time into textview
			tvDisplayTime.setText(
	                    new StringBuilder().append(pad(hour))
	                                       .append(":").append(pad(minute)));
			
			tvDisplayDate.setText(
                    new StringBuilder().append(pad(month+1))
                                       .append("/").append(pad(day)).append("/").append(pad(year)));
			
			due.setHour(hour);
			due.setMinute(minute);
			due.setDay(day);
			due.setMonth(month);
			due.setYear(year);
			
	 
		}
	 
		public void addListenerOnButton() {
	 
			ChangeTime = (Button) findViewById(R.id.time_button);
			ChangeDate = (Button) findViewById(R.id.day_button);
			
			ChangeTime.setOnClickListener(new OnClickListener() {
	 
				
				@SuppressWarnings("deprecation")
				public void onClick(View v) {
	 
					showDialog(TIME_DIALOG_ID);
	 
				}
	 
			});
			
			ChangeDate.setOnClickListener(new OnClickListener() {
	 
				
				@SuppressWarnings("deprecation")
				public void onClick(View v) {
	 
					showDialog(DATE_DIALOG_ID);
	 
				}
	 
			});
	 
		}
	 
		@Override
		protected Dialog onCreateDialog(int id) {
			switch (id) {
			case TIME_DIALOG_ID:
				// set time picker as current time
				return new TimePickerDialog(this, 
	                                        timePickerListener, hour, minute,false);
			case DATE_DIALOG_ID:
				return new DatePickerDialog(this, 
						datePickerListener, year, month, day);
			}
			return null;
		}
	 
		private TimePickerDialog.OnTimeSetListener timePickerListener = 
	            new TimePickerDialog.OnTimeSetListener() {
			public void onTimeSet(TimePicker view, int selectedHour,
					int selectedMinute) {
				hour = selectedHour;
				minute = selectedMinute;
	 
				// set current time into textview
				tvDisplayTime.setText(new StringBuilder().append(pad(hour))
						.append(":").append(pad(minute)));
	 
				due.setHour(hour);
				due.setMinute(minute);
				
	 
			}
		};
	 
		private DatePickerDialog.OnDateSetListener datePickerListener =
	            new DatePickerDialog.OnDateSetListener() {

	                public void onDateSet(DatePicker view, int Myear, 
	                                      int monthOfYear, int dayOfMonth) {
	                    year = Myear;
	                    month = monthOfYear;
	                    day = dayOfMonth;

	                    tvDisplayDate.setText(new StringBuilder().append(pad(month+1))
                                .append("/").append(pad(day)).append("/").append(pad(year)));
	                    
	                    
	        			due.setDay(day);
	        			due.setMonth(month);
	        			due.setYear(year);
	                    
	                }
	            };
		
		private static String pad(int c) {
			if (c >= 10)
			   return String.valueOf(c);
			else
			   return "0" + String.valueOf(c);
		}
}